import { _decorator, Component, Node, RigidBody2D, CircleCollider2D, Contact2DType, IPhysics2DContact, Vec3, v3, v2, Vec2, Sprite } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../Init/Managers/EventTypes';
import GlobalPool from '../../Init/Tools/GlobalPool';
const { ccclass, property } = _decorator;

@ccclass('Bullet2D')
export class Bullet2D extends BasicComponet {
    protected body: RigidBody2D = null;
    protected collider: CircleCollider2D = null;

    protected _lineVec = v2();
    protected _direc = v2();
    protected _atk = 0;
    protected _isFinish = false;

    // #region ------------流程-----------
    protected initSub() {
        this.body = this.node.getComponent(RigidBody2D);
        this.collider = this.node.getComponent(CircleCollider2D);
        this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

    }
    protected onEvents() {

    }

    protected setData(d: { p: Vec3, direc: Vec2, h: number, atk: number, parent: Node }) {
        this.node.parent = d.parent;
        this.node.active = true;
        this.node.setPosition(d.p);
        this._atk = d.atk;
        this._direc.set(d.direc).normalize();
        this._lineVec.set(this._direc).multiplyScalar(GlobalConfig.Bullet.spd);
        this.body.linearVelocity = this._lineVec;
        this.body.enabled = true;
        this._isFinish = this._lineVec.x == 0 && this._lineVec.y == 0; //速度=0 直接回收
        //
        let sp = this.node.getComponent(Sprite);
        if (sp) {
            sp.enabled = GlobalConfig.isShow2dCollider;
        }
        //创建3d子弹
        this.node[GlobalEnum.ExtralProp.curLineVec2d] = v2(this._lineVec);
        this.emit(EventTypes.Mapping3DEvents.SetBullets, { t: this.node.name, node: this.node, h: d.h, state: 0 });
    }

    public reset() {
        this.body.linearVelocity = v2(0);
        this._isFinish = false;
        this.body.enabled = false;
    }

    public update(dt) {
        if (GlobalTmpData.Game.isGamePause) return;
        
        if (this.node.position.y < -9999) {
            this._isFinish = true;
        }
        if (this._isFinish && this.body.enabled) {
            //回收
            this.body.enabled = false;
            GlobalPool.put(this.node);
        }
        this._lineVec.set(this._direc).multiplyScalar(GlobalConfig.Bullet.spd * dt * 60);
        this.body.linearVelocity = this._lineVec;
    }
    // #endregion

    // #region -------------事件-----------
    _tmpP = v3();
    protected onBeginContact(selfCollider: CircleCollider2D, otherCollider: CircleCollider2D, contact: IPhysics2DContact | null) {
        if (this._isFinish) return;

        switch (otherCollider.group) {
            //墙壁.障碍物.敌方建筑
            case 1 << 4:
            case 1 << 7:
            case 1 << 8:
                this._isFinish = true;
                //击中效果
                this._tmpP.x = this.node.position.x * GlobalConfig.Scale2DTo3D;
                this._tmpP.z = -this.node.position.y * GlobalConfig.Scale2DTo3D;
                this._tmpP.y = 1;
                this.emit(EventTypes.EffectEvents.show2DEffect, { t: GlobalEnum.Effect2DType.BulletBoom, p: this._tmpP });
                break;
            //平民、僵尸
            case 1 << 5:
                this.emit(EventTypes.EnemyEvents.HitPeople, otherCollider.node.uuid, (isShoot) => {
                    this._isFinish = isShoot;
                });
                break;
            case 1 << 6:
                this.emit(EventTypes.RoleEvents.ByAtk, {
                    uuid: otherCollider.node.uuid, atk: this._atk, cb: (isShoot) => {
                        this._isFinish = isShoot;
                    }
                });
                break;
            default:
                break;
        }

    }

    // #endregion
}

